package com.briup.ch12;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/17
 * @Description:
 */
public class Test01_Client {
    public static void main(String[] args) throws IOException {
        //1.创建客户端2.连接服务器3.获取服务器响应数据io流/4.关闭连接

        /*//方式一：创建同时连接服务器，服务器的Ip地址和端口号
        Socket client=new Socket("127.0.0.1",12121);//直接创建加连接

        client.close();

        //方式二：创建客户端后，再合适的时候连接服务器，服务器的Ip地址和端口号
        Socket client2=new Socket();

        client2.connect(new InetSocketAddress("127.0.0.1",12121));

        client2.close();

        //方式三：先指定服务器的IP地址和端口号，再指定本地IP地址和端口号
        Socket client3=new Socket("127.0.0.1",12121, InetAddress.getLocalHost(),34343);
        client3.close();*/
        Socket client=new Socket("127.0.0.1",12121);
        InputStream is=client.getInputStream();
        OutputStream op=client.getOutputStream();
        //客户端先打招呼
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(op));
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        bw.write("Hello,Server!"+client.getLocalPort());
        bw.newLine();
        System.out.println(br.readLine());
        bw.close();
        br.close();
        client.close();

    }
}
